package com.autumn.cloud.demo.consumer.utils;

import com.autumn.exception.ErrorLevel;
import com.autumn.web.vo.AbstractApiResponse;
import com.autumn.web.vo.ApiResponse;
import com.autumn.web.vo.ErrorInfo;

/**
 * <p>
 * </p>
 *
 * @description TODO
 * @author: 老码农
 * @create: 2020-12-11 17:37
 **/
public class HystrixUtils {

    /**
     * 熔断错误响应
     *
     * @param message   消息
     * @param <TResult> 结果
     * @return
     */
    public static <TResult> ApiResponse<TResult> hystrixErrorResponse(String message) {
        ApiResponse<TResult> response = new ApiResponse<>();
        response.setSuccess(false);
        ErrorInfo errorInfo = new ErrorInfo();
        errorInfo.setCode(ApiException.HYSTRIX_ERROR_CODE);
        errorInfo.setLevel(ErrorLevel.SYSTEM);
        errorInfo.setMessage(message);
        response.setError(errorInfo);
        return response;
    }

    /**
     * 创建 api 异常
     *
     * @param response 响应
     * @return
     */
    public static ApiException createApiException(AbstractApiResponse response) {
        return createApiException(response.getError());
    }

    /**
     * 创建 api 异常
     *
     * @param errorInfo 异常信息
     * @return
     */
    public static ApiException createApiException(ErrorInfo errorInfo) {
        return new ApiException(errorInfo.getCode(), errorInfo.getLevel(), errorInfo.getMessage());
    }
}
